package cn.imvc.app.models;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.util.matcher.RequestMatcher;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.GoodsAttrValue;
import cn.imvc.app.entities.GoodsImage;
import cn.imvc.app.repositories.GoodsAttrValueRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;

@Service
public class GoodsAttrValueModelImpl implements GoodsAttrValueModel {
	@Autowired
	GoodsAttrValueRepository goodsAttrValueRepository;
	@Autowired
	GoodsModel goodsModel;
	@Autowired
	GoodsClassAttrModel goodsClassAttrModel;

	public LinkedHashMap<RequestMatcher, Collection<ConfigAttribute>> getRequestMap() {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<GoodsAttrValue> find(ViewRequest viewRequest) {
		return goodsAttrValueRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public GoodsAttrValue get(int id) {
		return goodsAttrValueRepository.findOne(id);
	}

	public GoodsAttrValue save(GoodsAttrValue goodsAttrValue) {
		if(isHas(goodsAttrValue)){
			return null;
		}
		goodsAttrValue = goodsAttrValueRepository.saveAndFlush(goodsAttrValue);
		int goodsId= goodsAttrValue.getGoodsId();
		//更新类别属性关联表的信息，需放在保存属性值前
		goodsClassAttrModel.updateRelationByAttrClass(goodsAttrValue, 1);
		goodsModel.updateGoodsAttr(goodsId, goodsAttrValueRepository.findByGoodsId(goodsId));
		return goodsAttrValue;
	}

	public GoodsAttrValue update(GoodsAttrValue goodsAttrValue) {
		if(isHas(goodsAttrValue)){
			return null;
		}
		//更新类别属性关联表的信息，需放在保存属性值前
		goodsClassAttrModel.updateRelationByAttrClass(goodsAttrValue, 1);
		if(goodsAttrValue.getAttrValue().getId() == 0){
			goodsAttrValue.setAttrValue(null);
		}
		goodsAttrValue = goodsAttrValueRepository.saveAndFlush(goodsAttrValue);
		int goodsId= goodsAttrValue.getGoodsId();
		goodsModel.updateGoodsAttr(goodsId, goodsAttrValueRepository.findByGoodsId(goodsId));
		return goodsAttrValue;
	}

	public void delete(int id) {
		GoodsAttrValue goodsAttrValue = get(id);
		int goodsId= goodsAttrValue.getGoodsId();
		//更新类别属性关联表的信息，需放在保存属性值前
		goodsClassAttrModel.updateRelationByAttrClass(goodsAttrValue, 0);
		goodsAttrValueRepository.delete(id);
		goodsModel.updateGoodsAttr(goodsId, goodsAttrValueRepository.findByGoodsId(goodsId));
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
	}

	/**
	 * 判断是否已存在该属性
	 */
	public boolean isHas(GoodsAttrValue goodsAttrValue) {
		boolean isHas = false;
		if(goodsAttrValue.getId() != 0){
//			List<GoodsAttrValue> goodsAttrValues = goodsAttrValueRepository.findByGoodsIdAndAttrClass(goodsAttrValue.getGoodsId(), goodsAttrValue.getAttrClass());
			List<GoodsAttrValue> goodsAttrValues = goodsAttrValueRepository.findByGoodsIdAndAttrClassAndAttrValueName(goodsAttrValue.getGoodsId(), goodsAttrValue.getAttrClass(),goodsAttrValue.getAttrValueName());
			if(goodsAttrValues != null && !goodsAttrValues.isEmpty() && goodsAttrValues.get(0).getId() != goodsAttrValue.getId()){
				isHas = true;
			}
		}else{
//			List<GoodsAttrValue> goodsAttrValues = goodsAttrValueRepository.findByGoodsIdAndAttrClass(goodsAttrValue.getGoodsId(), goodsAttrValue.getAttrClass());
			List<GoodsAttrValue> goodsAttrValues = goodsAttrValueRepository.findByGoodsIdAndAttrClassAndAttrValueName(goodsAttrValue.getGoodsId(), goodsAttrValue.getAttrClass(),goodsAttrValue.getAttrValueName());
			if(goodsAttrValues != null && !goodsAttrValues.isEmpty()){
				isHas = true;
			}
		}
		return isHas;
	}

}
